﻿using System.Linq.Expressions;
using Basic.FormCollect.Model;
using Basic.FormModel.DB;
using LinqKit;
using WeDonekRpc.Helper;

namespace Basic.FormCollect.lmpl
{
    internal static class WhereLinq
    {
        public static Expression<Func<DBTableColumn, bool>> ToWhere ( this TableColGetParam param, long tableId )
        {
            ExpressionStarter<DBTableColumn> where = PredicateBuilder.New<DBTableColumn>(a => a.TableId == tableId);
            if ( !param.DataType.IsNull() )
            {
                where = where.And(a => param.DataType.Contains(a.DataType));
            }
            if ( !param.ColType.IsNull() )
            {
                where = where.And(a => param.ColType.Contains(a.ColType));
            }
            if ( !param.FuncType.IsNull() )
            {
                where = where.And(a => param.FuncType.Contains(a.FuncType));
            }
            return where;
        }
        public static Expression<Func<DBTableColumn, bool>> ToWhereByFormId ( this TableColGetParam param, long formId )
        {
            ExpressionStarter<DBTableColumn> where = PredicateBuilder.New<DBTableColumn>(a => a.FormId == formId);
            if ( !param.DataType.IsNull() )
            {
                where = where.And(a => param.DataType.Contains(a.DataType));
            }
            if ( !param.ColType.IsNull() )
            {
                where = where.And(a => param.ColType.Contains(a.ColType));
            }
            if ( !param.FuncType.IsNull() )
            {
                where = where.And(a => param.FuncType.Contains(a.FuncType));
            }
            return where;
        }
    }
}
